package mysqlconn;

import org.apache.commons.dbcp.BasicDataSource;

import java.sql.*;

public class MyDataSource {

    public static BasicDataSource ds = null;

    public static BasicDataSource init() {
        ConfigClient client = new ConfigClient();
        client.initConfig();
        //设置必须的参数
        ds = new BasicDataSource();
        ds.setDriverClassName(client.driver);
        ds.setUrl(client.url);
        ds.setUsername(client.user);
        ds.setPassword(client.password);
        //设置连接池的管理策略参数
        ds.setInitialSize(2);
        ds.setMaxActive(100);
        System.out.println("数据库连接池初始化:driver:" + client.driver + ",url:" + client.url + ", user:" + client.user + ", password:" + client.password);
        return ds;
    }

    public static void close() {
        try {
            if (ds!=null) {
                ds.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

}
